package com.jse.dao;

import java.util.List;

import com.jse.util.Json;
import com.jse.util.Strings;

import jdk.nashorn.api.scripting.ScriptObjectMirror;

public class Cnd {
	
	String sql="";

	public Cnd(String str) {
		this.sql=str;
	}

	public Cnd() {
	}
	
	public static Cnd NEW() {
		return new Cnd();
	}

	public static Cnd where(String name,String op,Object val) {
		return new Cnd().exp(" where ".concat(name), op, val);
	}
	
	public Cnd and(String name,String op,Object val) {
		return exp(name, op, val,"and ");
	}
	
	public Cnd and(Object str) {
		if(this.sql.contains(" order by")) {
			String where=this.sql.substring(0, sql.indexOf(" order by"));
			String order=this.sql.substring(sql.indexOf(" order by"));
			this.sql=where.concat(" and ").concat(str.toString()).concat(order);
		}else {
			this.sql+=" and ".concat(str.toString());
		}
		return this;
	}
	
	public Cnd or(String name,String op,Object val) {
		return exp(name, op, val,"or ");
	}
	
	public Cnd exp(String name,String op,Object val) {
		return exp(name, op, val,"");
	}
	
	public Cnd desc(String name) {
		if(sql.contains(" order by")) {
			sql=sql.substring(0, sql.indexOf(" order by")+10).concat(name).concat(" desc");
		}else {
			this.sql=sql.concat(" order by ").concat(name).concat(" desc");
		}
		return this;
	}
	
	public Cnd asc(String name) {
		if(sql.contains(" order by")) {
			sql=sql.substring(0, sql.indexOf(" order by")+10).concat(name);
		}else {
			this.sql=sql.concat(" order by ").concat(name);
		}
		return this;
	}
	
	public Cnd exp(String name,String op,Object val,String ex) {
		String prifx="";
		if(this.sql.contains("where")) {
			prifx=ex;
		}else {
			if(ex.equals("and ")) {
				this.sql+="where ";
			}
		}
		StringBuffer str=new StringBuffer(prifx).append(name);
		if(op.toLowerCase().contains("in")) {
			if(val.getClass().isArray()||val instanceof List) {
				String vals=Json.toJson(val).replace("[", "(").replace("]", ")").replace("\"", "'");
				str.append(" ").append(op).append(" ").append(vals.equals("()")?"(null)":vals).append(" ");
			}else if(val instanceof ScriptObjectMirror) {
				ScriptObjectMirror som = ScriptObjectMirror.class.cast(val);
				if (som.toString().equals("[object Array]")) {
					String vals=Json.toJson(val).replace("[", "(").replace("]", ")").replace("\"", "'");
					str.append(" ").append(op).append(" ").append(vals).append(" ");
				} else if (som.toString().equals("[object Object]")) {
					str.append(" ").append(op).append(" ").append(val).append(" ");
				}
			}else if(val instanceof Boolean) {
				Boolean boo=Boolean.class.cast(val);
				str.append(" ").append(op).append(" (").append(boo?1:0).append(") ");
			}else if(val instanceof Number) {
				str.append(" ").append(op).append(" (").append(val).append(") ");
			}else {
				if(val.toString().startsWith("(")&&val.toString().endsWith(")")) {
					str.append(" ").append(op).append(" ").append(val).append(" ");
				}else {
					str.append(" ").append(op).append(" (").append(val).append(") ");
				}
			}
		}else {
			if(val instanceof String) {
				str.append(" ").append(op).append(" '").append(val).append("' ");
			}else if(val instanceof Boolean) {
				Boolean boo=Boolean.class.cast(val);
				str.append(" ").append(op).append(" ").append(boo?1:0).append(" ");
			}else
			str.append(" ").append(op).append(" ").append(val).append(" ");
		}
		if(this.sql.contains(" order by")) {
			String where=this.sql.substring(0, sql.indexOf(" order by"));
			String order=this.sql.substring(sql.indexOf(" order by"));
			this.sql=where.concat(str.toString()).concat(order);
		}else {
			this.sql+=str.toString();
		}
		return this;
	}
	
	public static Cnd wrap(String str) {
		if(!Strings.isEmpty(str)&&!str.contains("where")) {
			str=" where ".concat(str);
		}
		return new Cnd(str);
	}

	@Override
	public String toString() {
		return Strings.isEmpty(sql)?"":sql.toString();
	}
	
	
}
